package com.wjh.test;

import com.wjh.bean.A1002;
import com.wjh.bean.Course;
import com.wjh.bean.Q1002;
import com.wjh.bean.Student;
import com.wjh.mapper.A1002Mapper;
import com.wjh.mapper.CourseMapper;
import com.wjh.mapper.Q1002Mapper;
import com.wjh.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class QAtest {


    private SqlSessionFactory factory = null;

    @Before
    public void init() throws IOException {
        String str = "mybatis-config.xml";
        InputStream in = Resources.getResourceAsStream(str);
        factory = new SqlSessionFactoryBuilder().build(in);

    }

    //一对多
    @Test
    public void test1() {
        SqlSession session = factory.openSession();
        Q1002Mapper mapper = session.getMapper(Q1002Mapper.class);


        List<Q1002> list = mapper.foreachanswer("nishishui?");
        System.out.println(list);

    }
//    @Test
//    public void test2(){
//            SqlSession session=factory.openSession();
//            Q1002 q1002=session.selectOne("com.wjh.mapper.Q1002Mapper.foreachanswer","nishishui?");
//            System.out.println(q1002);
//
//
//        }
//多对一
    @Test
    public void test3() {
        SqlSession session = factory.openSession();
        A1002 a1002 = session.selectOne("com.wjh.mapper.A1002Mapper.selectone", 2);
        System.out.println(a1002);
        System.out.println(a1002.getQ1002());
    }

    //多对多
    @Test
    public void test4() {
        SqlSession session = factory.openSession();
        StudentMapper mapper=session.getMapper(StudentMapper.class);
        Student s=mapper.selectByPrimaryKey(2);
        System.out.println(s);
        System.out.println(s.getCourses());
    }
    //二级缓存
    @Test
    public void test5(){
        SqlSession session = factory.openSession();
        CourseMapper mapper=session.getMapper(CourseMapper.class);
        Course course=mapper.selectByPrimaryKey(1l);
        Course course1=mapper.selectByPrimaryKey(1l);
        System.out.println(course);
        System.out.println(course1);

        session.commit();
        session.close();
//        SqlSession session1 = factory.openSession();
//        CourseMapper mapper1= session1.getMapper(CourseMapper.class);
//        Course course1=mapper1.selectByPrimaryKey(1l);
//        System.out.println(course1);
        //System.out.println(course.getStudents());
    }

}
